import HttpApi from './api';
import Toast from 'react-native-root-toast';
class HttpServer {
    constructor() { }
    static get instance() {
        if (!HttpServer._instance) {
            Object.defineProperty(HttpServer, '_instance', {
                value: new HttpServer()
            });
        }
        return HttpServer._instance;
    }
    handleSuccessResponse = (res) =>{
        switch (res.code) {
            case 401:
                Toast.show('用户登陆已过期', {
                    duration: Toast.durations.LONG, // toast显示时长
                    position: Toast.positions.CENTER // toast位置
                });
                return;
            case 500:
                Toast.show('服务器出现问题', {
                    duration: Toast.durations.LONG, // toast显示时长
                    position: Toast.positions.CENTER // toast位置
                });
                return;
        }
    }
    handleErro = (res) =>{
        switch (res.status) {
            case 401:
                Toast.show('用户登陆已过期', {
                    duration: Toast.durations.LONG, // toast显示时长
                    position: Toast.positions.CENTER // toast位置
                });
                return {'status':401,'msg':'用户登陆已过期'};
            case 500:
                Toast.show('服务器出现问题', {
                    duration: Toast.durations.LONG, // toast显示时长
                    position: Toast.positions.CENTER // toast位置
                });
                return {'status':500,'msg':'服务器出现问题'};
            default:
                return {'status':5000,'msg':'无网络链接'};
        }
    }
    get(url,showErro = false) {
        return new Promise((resolve, reject) => {
            fetch(HttpApi.BaseUrl + url)
                .then(response => {
                    if (response.ok) {
                        return response.text();
                    } else {
                        let resE = this.handleErro(response);
                        return Promise.reject(resE);
                    }
                })
                .then(responseText => {
                    const responseJSObj = JSON.parse(responseText);
                    this.handleSuccessResponse(responseJSObj);
                    resolve(responseJSObj);
                })
                .catch(error => {
                    if(showErro){
                        reject(error);
                    }
                });
        });
    }

    post(url, params = {},showErro = false) {
        return new Promise((resolve, reject) => {
            fetch(HttpApi.BaseUrl + url, {
                method: 'POST',
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(params)
            })
                .then(response => {
                    if (response.ok) {
                        return response.text();
                    } else {
                        let resE = this.handleErro(response);
                        return Promise.reject(resE);
                    }
                })
                .then(responseText => {
                    // 然后把JSON字符串序列化为JS对象
                    const responseJSObj = JSON.parse(responseText);
                    this.handleSuccessResponse(responseJSObj);
                    // 把请求成功的数据传递出去
                    resolve(responseJSObj);
                })
                .catch(error => {
                    if(showErro){
                        reject(error);
                    }
                });
        });
    }
}
export default HttpServer.instance;
